package com.edu.web.chat;

import com.edu.entity.chat.*;
import com.edu.entity.user.EduUser;
import com.edu.entity.user.SysUser;
import com.edu.service.chat.EduChatService;
import com.edu.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: jzhang
 * @Date: 2018/10/15 15:51
 * @Description:    后台主页控制台
 */
@Controller
public class MenuController {
    private static final int PAGE_COUNT = 4;


    @Autowired
    private EduChatService eduChatService;

    @Autowired
    private UserService userService;


    @RequestMapping("/chathome")
    public String index(HttpServletRequest request, Model model,Principal principal){
        int id = userService.findUserByName(principal.getName()).getId();
        int currentPage = 1;
        String page = request.getParameter("page");
        if(page != null){
            currentPage = Integer.valueOf(page);
        }
        List<Chat> list = eduChatService.findchatlist(new Page(id,(currentPage-1) * PAGE_COUNT,PAGE_COUNT));
        List<Chat> newlist = eduChatService.findchatnewlist();
        int totalPage = eduChatService.getTotal(PAGE_COUNT);
        model.addAttribute("list",list);
        model.addAttribute("newlist",newlist);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage","?page="+(currentPage+1));
        return "chat/index";
    }

    @RequestMapping("/chatinfo")
    public String chatinfo(int id,Model model,Principal principal){
        int id_ = userService.findUserByName(principal.getName()).getId();
        Chat chat = eduChatService.getchatinfo(id);
        EduUser eduUser = userService.findUserById(chat.getUser_id());
        List<Chat> newlist = eduChatService.findchatnewlist();
        String name = eduUser.getUsername();
        eduChatService.updatechat(new Updatechat(id,new Date()));
        List<Buy> buy = eduChatService.findbuylist(chat.getId());
        int i = 0;
        if(chat.getEndnumber() <= chat.getNownumber()){
            i = 2;
        }
        for(Buy buy1 : buy) {
            if (buy1.getBuyer_id() == id_) {
                System.out.println(buy1);
                i = 1;
            }
        }
        model.addAttribute("chat",chat);
        model.addAttribute("name",name);
        model.addAttribute("money",i !=2 ? i == 0 ? "预 定  ￥" +chat.getMoney():"已 预 订 " : "人数已满");
        model.addAttribute("button",i==0 ? false : true );
        model.addAttribute("newlist",newlist);
        return "chat/chatshow";
    }

    @RequestMapping("/chatreserve")
    public String chatreserve(int id,Principal principal){
        int id_ = userService.findUserByName(principal.getName()).getId();
        Buy buy = new Buy();
        buy.setChat_id(id);
        buy.setBuyer_id(id_);
        buy.setCreatetime(new Date());
        int i = eduChatService.reservechat(buy);
        eduChatService.chatnownumber(buy.getChat_id());
        return "redirect:chatinfo?id="+id;
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model,String keyword,Principal principal){
        int id = userService.findUserByName(principal.getName()).getId();
        if(keyword == null){
            keyword = "";
        }
        int currentPage = 1;
        String page = request.getParameter("page");
        if(page != null){
            currentPage = Integer.valueOf(page);
        }
        List<Chat> list = eduChatService.findchatnamelist(new Page(id,(currentPage-1) * PAGE_COUNT,PAGE_COUNT,"%"+keyword+"%"));
        System.out.println(list.toString());
        List<Chat> newlist = eduChatService.findchatnewlist();
        int totalPage = eduChatService.getTotal(PAGE_COUNT);
        model.addAttribute("list",list);
        model.addAttribute("newlist",newlist);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage","?page="+(currentPage+1)+"&keyword="+keyword);
        return "chat/list";
    }
    @RequestMapping("/createchat")
    public String createchat(Model model) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 7);
        model.addAttribute("createdate", format.format(calendar.getTime()));
        calendar.add(Calendar.DATE, 7);
        model.addAttribute("enddate", format.format(calendar.getTime()));
        List<Chat> newlist = eduChatService.findchatnewlist();
        model.addAttribute("newlist",newlist);
        return "chat/createchat";
    }
    @RequestMapping("/savechat")
    public String savechat(@RequestParam("file") MultipartFile file, ModelMap map, Chat chat, Principal principal) throws IOException {
        int id = userService.findUserByName(principal.getName()).getId();
       chat.setUser_id(id);
        String filePath = "E:\\upload";//保存图片的路径
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        //新的文件名字
        String newFileName = UUID.randomUUID()+originalFilename;
        File targetFile = new File(filePath,newFileName);
        file.transferTo(targetFile);
        System.out.println(targetFile.toString());
        chat.setPichat(newFileName);
        System.out.println(chat.toString());
        int i = eduChatService.insertchat(chat);
        return "redirect:chathome";
    }

    @RequestMapping("/chatlist")
    public String chatlist(Principal principal,Model model){
        int id = userService.findUserByName(principal.getName()).getId();
        List<Chat> list = eduChatService.finduserchatlist(id);
        System.out.println(list.toString());
        System.out.println(list);
        model.addAttribute("list",list);
        return "chat/chatlist";
    }

    @RequestMapping("/createword")
    public String createword(int id,Model model,Principal principal){
        int id_ = userService.findUserByName(principal.getName()).getId();
        model.addAttribute("chatid",id);
        model.addAttribute("userid",id_);
        return "chat/createword";
    }
    @RequestMapping("/saveblog")
    public String saveword(Essay essay){
        essay.setCreatetime(new Date());
        System.out.println(essay.toString());
        eduChatService.insertessay(essay);
        eduChatService.updatechatessay(essay.getChatid());
        return "redirect:chatlist";
    }
    @RequestMapping("/chatreservelist")
    public String chatreserve(Principal principal,Model model){
        int id = userService.findUserByName(principal.getName()).getId();
        List<Chat> list = eduChatService.findchatreserve(id);
        List<Chat> newlist = eduChatService.findchatnewlist();
        model.addAttribute("list",list);
        model.addAttribute("newlist",newlist);
        return "chat/chatreserve";
    }
    @RequestMapping("/chatroom")
    public String chat(Model model,int id,Principal principal){
        int uid = userService.findUserByName(principal.getName()).getId();
        String username = principal.getName();
        Essay essay = eduChatService.essayshow(id);
        if(uid == essay.getUserid()){
            model.addAttribute("username","作者");
        }else{
            model.addAttribute("username",username);
        }
        model.addAttribute("essay",essay);
        return "chat/chatessay";
    }
    @RequestMapping("/chateassylist")
    public String chateassylist(Principal principal,Model model){
        int id = userService.findUserByName(principal.getName()).getId();
        List<Chat> list = eduChatService.findusereassylist(id);
        List<Chat> newlist = eduChatService.findchatnewlist();
        System.out.println(list.toString());
        System.out.println(list);
        model.addAttribute("list",list);
        model.addAttribute("newlist",newlist);
        return "chat/chateassyinfolist";
    }
}
